<?php

/* @var $this yii\web\View */
use yii\helpers\Html;
use yii\helpers\Url;
use yii\grid\GridView;
use app\models\Member;
use yii\data\ActiveDataProvider;

$this->title = '编辑K线图数据';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="page-title">
  <div>
    <h1><i class="fa fa-users"></i> 编辑K线数据</h1>
    <ul class="breadcrumb side">
      <li><i class="fa fa-home fa-lg"></i></li>
      <li>NEA走势图</li>
      <li class="active"><a href="#">编辑K线数据</a></li>
    </ul>
  </div>
</div>
<div class='card'>
  <div class="btn-group pull-right">
    <a class="btn btn-info btn-sm" href='<?=Url::toRoute('site/configkline')?>' style='margin:0 5px;'><i class="fa fa-wrench"></i> K线配置</a>
    <a class="btn btn-info btn-sm" id='del_all_btn' style='cursor:pointer;margin:0 5px;'><i class="fa fa-trash"></i> 全部删除</a>
    <a class="btn btn-info btn-sm" href='<?=Url::toRoute('site/addklineitem')?>' style='margin:0 5px;'><i class="fa fa-plus-circle"></i> 添加</a>
  </div>
<?php
//如何提前加载资源
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// 数据提供者中所含数据所定义的简单的列
// 使用的是模型的列的数据
'data_date',
'data_open',
'data_close',
'data_highest',
'data_lowest',
'data_amount',
'data_base',
[
  'header'=>'是否账户划拨',
  'content'=>function ($model, $key, $index, $column) {
    if($model->is_fenhong_point){
      return '是';
    }else{
      return '否';
    }
  },
],
[
  'class' => 'yii\grid\ActionColumn',
  'header'=>'编辑',
  'template'=>'{edit} {del}',
  'buttons'=>[
      'edit'=> function ($url, $model, $key) {
        return '<a href="'.Url::toRoute(['site/editklineitem','id'=>$model->id]).'" style="cursor:pointer;"><i class="fa fa-lg fa-edit"></i></a>';
      },
      'del'=> function ($url, $model, $key) {
        return '<a onclick="delklineitem('.$model->id.')" style="cursor:pointer;"><i class="fa fa-lg fa-trash-o"></i></a>';
      },
  ],
      // you may configure additional properties here
],

// 更复杂的列数据
/*[
  'class' => 'yii\grid\DataColumn', //由于是默认类型，可以省略
  'value' => function ($data) {
      return $data->name; // 如果是数组数据则为 $data['name'] ，例如，使用 SqlDataProvider 的情形。
  },
],*/
],

]);
?>
</div>

<!-- Modal -->
<div class="modal fade" id="delAllConfirm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog modal-sm" role="document">
    <div class="modal-content">
      <div class="modal-header btn-warning">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel"><i class="fa fa-trash"></i> 删除所有的K线数据</h4>
      </div>
      <div class="modal-body">
          <p>您确定要删除所有的K线数据吗？</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
        <button id='delallconfirm_submit' type="button" class="btn btn-warning">确定</button>
      </div>
    </div>
  </div>
</div>

<?php $this->beginBlock('delall-js') ?>
function delklineitem(id){
  $.ajax({
    url:"<?=Url::toRoute('site/delklineitem')?>",
    type:"get",
    datatype:"json",
    cache:false,
    data:{
      id:id
    },
    success:function(rtn){
       var obj = JSON.parse(rtn);
       if(obj.errcode===0){
        location.href="<?=Url::toRoute('site/editkline')?>";
       }else{
         alert('未删除成功。');
       }
    },
    error:function(jqXHR,textStatus,errorThrown){
      console.log(jqXHR);
      console.log(textStatus);
      console.log(errorThrown);
    }
  });
}

$('#del_all_btn').click(function(){
  $('#delAllConfirm').modal('show');
});

$("#delallconfirm_submit").click(function(){
  $.ajax({
    url:"<?=Url::toRoute('site/delallkline')?>",
    type:"get",
    datatype:"json",
    cache:false,
    data:{
    },
    success:function(rtn){
       var obj = JSON.parse(rtn);
       if(obj.errcode===0){
        location.href="<?=Url::toRoute('site/editkline')?>";
       }else{
         alert('未删除成功。');
         $("#delAllConfirm").modal('hide');
       }
    },
    error:function(jqXHR,textStatus,errorThrown){
      console.log(jqXHR);
      console.log(textStatus);
      console.log(errorThrown);
      $("#delAllConfirm").modal('hide');
    }
  });
});
<?php $this->endBlock() ?>
<?php $this->registerJs($this->blocks['delall-js'],\yii\web\View::POS_END); ?>
